package com.pengxun.manager.service.system.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pengxun.manager.dao.system.MenuDao;
import com.pengxun.manager.model.system.Menu;
import com.pengxun.manager.model.system.Permission;
import com.pengxun.manager.model.system.RoleMenu;
import com.pengxun.manager.service.system.MenuService;
import com.pengxun.manager.service.system.PermissionService;
import com.pengxun.manager.service.system.RoleMenuService;
import com.szeastroc.common.jdbc.page.Page;
import com.szeastroc.common.utils.CodeUtils;


@Service
public class MenuServiceImpl implements MenuService{

	@Resource
	private MenuDao menuDao;
	@Resource
	private RoleMenuService roleMenuService;
	@Resource
	private PermissionService permissionService;
	
	@Override
	public int insert(Menu menu) {
		if(StringUtils.isNotBlank(menu.getParentMenuCode())&&!menu.getParentMenuCode().equals("0")) {
			menu.setMenuType(1);
		}else {
			menu.setParentMenuCode("0");//一级菜单的父菜单默认为0
			menu.setMenuType(0);//默认为一级菜单
		}
//		Menu lastMenu = selectLastMenuCode();
//		if(null==lastMenu) {
//			menu.setMenuCode(CodeUtils.generateCode("menuCode", 6, "menuCode000000"));
//		}else {
//			menu.setMenuCode(CodeUtils.generateCode("menuCode", 6, lastMenu.getMenuCode()));
//		}
		String pageUrl = menu.getPageUrl();
		if(StringUtils.isNotBlank(pageUrl)) {
			if(pageUrl.startsWith("/")) {
				pageUrl=pageUrl.substring(1);
			}
			String menuCode = pageUrl.replace("/", ".");
			menu.setMenuCode(menuCode);
		}
		return menuDao.insert(menu);
	}
	
    @Override
    public int update(Menu menu){
    	if(StringUtils.isNotBlank(menu.getParentMenuCode())&&!menu.getParentMenuCode().equals("0")) {
			menu.setMenuType(1);
		}else {
			menu.setParentMenuCode("0");//一级菜单的父菜单默认为0
			menu.setMenuType(0);//默认为一级菜单
		}
        return menuDao.update(menu);
    }
   	
    @Override
    public int delete(Integer id){
    	Menu menu = menuDao.findById(id);
    	//删除分配的权限
    	roleMenuService.deleteByMenuCode(menu.getMenuCode());
    	//删除菜单下的权限
    	permissionService.deleteByMenuCode(menu.getMenuCode());
    	//删除子菜单
    	menuDao.deleteByParentMenuCode(menu.getMenuCode());
    	
       return menuDao.delete(id);
 	}
   
   @Override
    public Page findByPage(Page page,String menuName){
    	 List<Menu> menus = menuDao.findByPage(page,menuName);
    	 page.setData(menus);
         return page;
    }

	@Override
	public Menu findById(Integer menuId) {
		return menuDao.findById(menuId);
	}

	@Override
	public Menu findByMenuCode(String menuCode) {
		return menuDao.findByMenuCode(menuCode);
	}

	
	@Override
	public List<Menu> findByMenuCodes(List<String> menuCodes){
		return menuDao.findByMenuCodes(menuCodes);
	}

	@Override
	public List<Menu> findByParentMenuCode(String parentMenuCode) {
		return menuDao.findByParentMenuCode(parentMenuCode);
	}
	
	public List<Menu> findAll(){
		//查询所有一级菜单,一级菜单的parentCode默认为0
		List<Menu> firstList = findByParentMenuCode("0");
		for (Menu menu : firstList) {
			List<Menu> secondList = findByParentMenuCode(menu.getMenuCode());
			for (Menu secondMenu : secondList) {
				List<Menu> thirdList = findByParentMenuCode(secondMenu.getMenuCode());
				secondMenu.setChildren(thirdList);
			}
			menu.setChildren(secondList);
		}
		return firstList;
	}

	@Override
	public int deleteByParentMenuCode(String parentMenuCode) {
		return menuDao.deleteByParentMenuCode(parentMenuCode);
	}

	@Override
	public List<Menu> findAllMenu() {
		return menuDao.findAllMenu();
	}

	@Override
	public JSONObject findMenuAuthority() {
		JSONObject jsonObject = new JSONObject();
		List<Menu> menuList = findAllMenu();
		for (Menu menu : menuList) {
			JSONObject menuJson = new JSONObject(); 
			List<RoleMenu> roleMenuList = roleMenuService.findByMenuCode(menu.getMenuCode());
			if(CollectionUtils.isEmpty(roleMenuList)) {
				continue;
			}
			JSONArray jsonArray = new JSONArray();
			for (RoleMenu roleMenu : roleMenuList) {
				jsonArray.add(roleMenu.getRoleCode());
			}
			menuJson.put("authority", jsonArray);
			jsonObject.put(menu.getPageUrl(), menuJson);
		}
		return jsonObject;
	}

	@Override
	public Menu selectLastMenuCode() {
		return menuDao.selectLastMenuCode();
	}
	
	
	public List<Menu> findMenuTree(){
		//查询所有一级菜单,一级菜单的parentCode默认为0
		List<Menu> firstList = findByParentMenuCode("0");
		for (Menu menu : firstList) {
			List<Menu> secondList = findByParentMenuCode(menu.getMenuCode());
			if(CollectionUtils.isEmpty(secondList)) {
				List<Permission> permissionList = permissionService.findByMenuCode(menu.getMenuCode());
				menu.setPermissions(permissionList);
				continue;
			}
			for (Menu secondMenu : secondList) {
				List<Menu> thirdList = findByParentMenuCode(secondMenu.getMenuCode());
				if(CollectionUtils.isEmpty(thirdList)) {
					List<Permission> permissionList = permissionService.findByMenuCode(secondMenu.getMenuCode());
					secondMenu.setPermissions(permissionList);
					continue;
				}
				for (Menu thirdMenu : thirdList) {
					List<Permission> permissionList = permissionService.findByMenuCode(thirdMenu.getMenuCode());
					thirdMenu.setPermissions(permissionList);
				}
				secondMenu.setChildren(thirdList);
			}
			menu.setChildren(secondList);
		}
		return firstList;
	}

	@Override
	public int updateStatus(Integer menuId, Integer status) {
		return menuDao.updateStatus(menuId, status);
	}

	@Override
	public List<Menu> findByMenuName(String menuName) {
		List<Menu> menuList = menuDao.findByMenuName(menuName);
		
		//查询所有一级菜单,一级菜单的parentCode默认为0
		List<Menu> firstList = findByParentMenuCode("0");
		for (Menu menu : firstList) {
			List<Menu> secondList = findByParentMenuCode(menu.getMenuCode());
			for (Menu secondMenu : secondList) {
				List<Menu> thirdList = findByParentMenuCode(secondMenu.getMenuCode());
				if(!CollectionUtils.isEmpty(thirdList)) {
					for (Menu thirdMenu : thirdList) {
						if(!menuList.contains(thirdMenu)) {
							thirdList.remove(thirdMenu);
						}
					}
				}
				if(CollectionUtils.isEmpty(thirdList)&&!menuList.contains(secondMenu)) {
					secondList.remove(secondMenu);
				}else {
					secondMenu.setChildren(thirdList);
				}
			}
			if(CollectionUtils.isEmpty(secondList)&&!menuList.contains(menu)) {
				firstList.remove(menu);
			}else {
				menu.setChildren(secondList);
			}
		}
		return firstList;
	}
	
}
